﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionCore;
using PCBase.UIControls;

namespace COER
{
    public partial class FormPortData : Form
    {
        public FormPortData()
        {
            InitializeComponent();

            GetControls(this.Controls);
        }

        private void GetControls(Control.ControlCollection ctrls)
        {
            foreach (Control item in ctrls)
            {
                if (item is Button && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((Button)item).Click += new EventHandler(this.btn_Switch_Click);
                }

                if (item is DLLPC.PCAP.PCButton && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((DLLPC.PCAP.PCButton)item).OP = Kernel.OP;
                }

                if (item.Controls != null)
                {
                    GetControls(item.Controls);
                }
            }
        }

        private int modelNo;
        private PortInfo portData;
        public void ShowForm(int idx, PortInfo info)
        {
            portData = info;
            propertyGrid1.SelectedObject = portData;
            modelNo = idx;
            switch (modelNo)
            {
                case 1:
                    {
                        this.Text = "Port A";
                    }
                    break;
                case 2:
                    {
                        this.Text = "Port B";
                    }
                    break;
            }

            this.Show();
            this.timer1.Start();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        int timecot = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Visible == false) return;

            btnDel.Monitor(false, ProcedureHelper_EQ.ManualMode);

            if (timecot > 30)
            {
                propertyGrid1.Refresh();
                timecot = 0;
            }
            timecot++;
        }

        private void btn_Switch_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string[] info = btn.Tag.ToString().Split('|');

            switch (info[1])
            {
                case "Del":
                    {
                        portData = new PortInfo();                       
                        this.Visible = false;
                    }
                    break;
            }
        }

        private void FormChipData_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Visible = false;
            this.timer1.Stop();
            e.Cancel = true;
        }
    }
}
